<template>
  <div class="container-cc">
    <div class="box" v-if="loadFlag">
      <div class="title">{{ newsTitle }}</div>
      <div class="item-msg">
        <div class="author">{{ issue }}</div>
        <div class="time">{{ issueTime }}</div>
      </div>
      <div class="scroll-box">
        <div class="video-box" v-if="news.isVideoFile">
          <VideoWrap :source="videoSource" height="22rem" />
        </div>
        <div>
          <span class="ql-editor" v-html="newsContent"></span>
        </div>
      </div>
    </div>

    <div class="loading-container" v-if="loadingiconFlag">
      <van-loading color="#1989fa" type="spinner">加载中...</van-loading>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { reactive } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { onMounted } from 'vue'
import { getMessageList, getMessageDetail } from '@/api/message';
import { showToast } from 'vant';
import { usePageTitle, useDictSelect } from '@/store/base.js';
const router = useRouter()
const news = ref({})
const issue = ref('')
const issueTime = ref('')
const newsTitle = ref('')
// const newsImage = ref('')
const newsContent = ref('')
const messageList = ref([])
const loadFlag = ref(false);
const loadingiconFlag = ref(false);
const videoSource = ref('');
const route = useRoute();
const curStatus = ref(route.query.status || '');
console.log(curStatus.value)

//  枚举 记录当前所有的选择
const pageMenu = {
  jzwk: {
    text: '家长微课',
    tag: '009',
  },
  jjzx: {
    text: '资讯详情',
  },
  hstj: {
    text: '推荐内容',
    tag: '007',
  },
  yexd: {
    text: '心得交流',
    tag: '006',
  },
}

const { pageTitle, setPageTitle } = usePageTitle();


//  初始化数据
function initData() {
  const { text, tag } = pageMenu[curStatus.value];
  //  设置标题
  setPageTitle(text);

}

const getDetail = () => {
  loadingiconFlag.value = true
  getMessageDetail(router.currentRoute.value.query.id).then((res) => {
    if (res.code === 200) {
      news.value = res.data
      issue.value = news.value.issue
      newsTitle.value = news.value.newsTitle
      issueTime.value = news.value.issueTime
      newsContent.value = news.value.newsContent

      try {
        const dd = JSON.parse(news.value.videoFile)[0];
        news.value.isVideoFile = dd;
        videoSource.value = process.env.VUE_APP_API_BASE_URL + '/spgl/SpdbVideo/getVideo?fileId=' + (dd.fileId || '');
      } catch(ev) {
        videoSource.value = '';
      }

      // if(JSON.parse(news.value.newsImage).length>0){
      //   newsImage.value = process.env.VITE_API_API_IMAGE_URL + JSON.parse(news.value.newsImage)[0].url
      // }
      loadFlag.value = true
      loadingiconFlag.value = false
    } else {
      showToast(res.msg)
      loadingiconFlag.value = false
    }
  }).catch((err) => {
    console.log(err)
  })
}

// const getList = () => {
//     const params = {
//       currentPage: 1,
//       pageSize: 100,
//       sidx: "issueTime",
//       sort: "desc",
//       issue: '',
//     }
//     getMessageList(params).then((res) => {
//     if (res.code === 200) {
//       if(res.data.list.length!=0){
//           messageList.value = res.data.list
//           messageList.value.map(s => {
//           s.newsImage = process.env.VITE_API_API_IMAGE_URL + JSON.parse(s.newsImage)[0].url
//           return s
//         })
//         imgFlag.value = false
//       }else{
//         messageList.value = []
//         imgFlag.value = true
//       }

//     } else {
//       showToast(res.msg)
//     }
//   }).catch((err) => {
//     console.log(err)
//   })
// }

const toDetail = (id) => {
  console.log('detail-id:::', id);
  router.push({ path: '/MessageDetail', query: { id: id } })
}

onMounted(() => {
  initData()
  getDetail()
})
</script>


<style lang="scss" scoped>

.scroll-box {
  flex: 1;
  overflow: scroll;
  box-sizing: border-box;
}

.container-cc {
  display: flex;
  flex-direction: column;
  background: #fff;
  overflow: hidden;
  box-sizing: border-box;
  margin-bottom: 0;
  padding: 0 1.4rem;
  height: 100%;
}


.ellipsis-multiline {
  word-break: break-all;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.title {
  font-size: 2rem;
  font-weight: bold;
}

.item-msg {
  font-size: 1.2rem;
  padding: 1rem 0;
}

.author {
  margin-bottom: 1rem;
}

.time {
  color: #ccc;
}

.newscontent {
  font-size: 1.5rem;
  text-indent: 2ch;
  line-height: 2.5rem;
  padding: 1rem 0;
}

.mess-image {
  width: 100%;
  margin-top: 1rem;
}

.index-image {
  width: 12rem;
  height: 9rem;
  margin-right: 2rem;
  border-radius: .8rem;
}

.index-title {
  font-size: 1.4rem;
  margin-bottom: 5rem;
}

.index-msg {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: .6rem;
  color: #aaa;
  font-size: 1.2rem;
}

.index-time {
  margin-top: 0.2rem;
}

.index-box {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 1rem;
  margin-top: 2rem;
  border-top: 1px solid #ccc;
  border-radius: 1rem;
  box-sizing: border-box;
  background: #fff;
  padding: 2rem 1.4rem 1rem;
}

.box-right {
  width: 20rem;
}

.box {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 1rem 1.4rem;
  border-radius: 1rem;
  height: 100%;
  box-sizing: border-box;
}


.wxContent {
  display: block;
  max-height: 66vh;
  overflow: auto;
}

::v-deep img {
  max-width: 100%;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 15rem;
  left: 40%;
}
</style>
